package com.le.tester.leetcode;

import com.le.tester.leetcode.common.TreeNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * currentTime:2022/3/10 11:14
 * description: 后序遍历
 */
public class PostOrderTraversal {
    public List<Integer> preOrderTraversal(TreeNode root) {

        List<Integer> resultList = new ArrayList<>();
        this.preOrder(root, resultList);
        //Collections.reverse(resultList);
        return resultList;
    }

    public void preOrder(TreeNode root, List<Integer> integerList) {
        if (Objects.isNull(root)) {
            return;
        }

        //前序的遍历形式
        //integerList.add(root.val);
        //this.preOrder(root.left, integerList);
        //this.preOrder(root.right, integerList);


        //遍历顺序修改（后序）
        this.preOrder(root.left, integerList);
        this.preOrder(root.right, integerList);
        integerList.add(root.val);
    }
}
